/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wb.picmover.handler;

import cn.ac.iie.di.wb.picmover.util.FtpTools;
import cn.ac.iie.di.wb.picmover.util.GlobalParameters;
import java.io.File;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class PicThreadPoolTask1 implements Runnable {

    static private final Logger logger = Logger.getLogger(PicThreadPoolTask1.class.getName());
    private Message list;

    public PicThreadPoolTask1(Message list) {
        this.list = list;
    }

    @Override
    public void run() {
//        logger.info("get " + list.getData().size() + " datas form picHandlerQueue!");
//        long starttime = System.currentTimeMillis() / 1000;
//        FtpTools ftpTools = new FtpTools();
//        FTPClient createFtpClient = ftpTools.createFtpClient(GlobalParameters.ftpip, GlobalParameters.ftpport, GlobalParameters.username, GlobalParameters.password);
//
//        int addColId = 0;
//        int pathColId = 0;
//        String[] colNames = list.getColNames();
//        for (int id = 0; id < colNames.length; id++) {
//            if (colNames[id].equals(GlobalParameters.addColName)) {
//                addColId = id;
//            } else if (colNames[id].equals(GlobalParameters.pathColName)) {
//                pathColId = id;
//            }
//        }
//
//        int downnum = 0;
//        int copynum = 0;
//        List<Object[]> dataList = list.getData();
//        try {
//            for (int i = 0; i < dataList.size(); i++) {
//                Object[] dataArr = dataList.get(i);
//                String[] picArr = (String[]) dataArr[addColId];
//                String[] pathArr = (String[]) dataArr[pathColId];
//                for (int re = 0; re < picArr.length; re++) {
//                    String[] picStr = picArr[re].split("[|]");
//                    if (picStr[0].equals("0")) {
//                        //需要下载文件
//                        boolean downFile = ftpTools.downDataFile(createFtpClient, GlobalParameters.ftpip, GlobalParameters.filepath, GlobalParameters.localpath, picStr[1]);
//                        if (!downFile) {
//                            logger.info("====download " + picStr[1] + " failed.");
//                        } else {
//                            downnum++;
//                        }
//                    } else {
//                        //不需要下载文件
//                        File picfile = null;
//                        File newpicfile = null;
//                        try {
//                            picfile = new File(GlobalParameters.localpath + "/" + picStr[1]);
//                            newpicfile = new File(GlobalParameters.localpath + "/" + pathArr[re]);
//                            FileUtils.copyFile(picfile, newpicfile);
//                            copynum++;
//                        } catch (Exception e) {
//                            logger.error("copy " + picfile.getAbsolutePath() + " to " + newpicfile.getAbsolutePath() + " failed,cause " + e.getMessage(), e);
//                        }
//                    }
//
//                }
//            }
//        } catch (Exception e) {
//            logger.error("download file or copy file failed,cause " + e.getMessage(), e);
//        } finally {
//            //关闭ftp连接
//            if (createFtpClient != null) {
//                ftpTools.destroyFtpClient(createFtpClient);
//            }
//        }
//
//        logger.info("download ftp file size " + downnum + " and use time " + (System.currentTimeMillis() / 1000 - starttime) + " s");
//        logger.info("copy localfile size " + copynum + " and use time " + (System.currentTimeMillis() / 1000 - starttime) + " s");
//        while (GlobalParameters.picSendQueue.offer(list)) {
//            break;
//        }
//        logger.info("offer " + dataList.size() + " to picSendHandlerQueue!");

    }

}
